package is2_domestik

class User {

    transient springSecurityService
    
    String nombre;
    String apellido;
    String telefono;
    String celular;
    Date fechaDeNacimiento;
    String ciudad;
    Imagen imagen;   
    String username
    String password
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    static constraints = {
        imagen nullable: true
        username blank: false, unique: true, email:true
        password blank: false
        nombre (blank:false, size: 3..20)
        apellido (blank:false, size: 3..20)
        telefono (blank: false, size: 7..7, matches: "[0-9]+")
        celular (blank: false, size: 10..10, matches: "[0-9]+")
        fechaDeNacimiento (blank: false, max: new Date())
        ciudad   (blank: false, inList:["Bogota","Cali"])
    }

    static mapping = {
        password column: '`password`'
    }

    Set<UserRole> getAuthorities() {
        UserUserRole.findAllByUser(this).collect { it.userRole } as Set
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}
